const User = require('../../../models/blog_user');
const Article = require('../../../models/blog_article');
const Talk = require('../../../models/blog_talk');
const Comment = require('../../../models/blog_comment');
const Util = require('../../../utils');
const utils = require('../../../utils');

/**
 * 数据中心控制器
 */
const ControllerHome = {
    getList: async function (req, res, next) {
        try {
            const users = await User.all();
            const articles = await Article.all();
            const talks = await Talk.all();
            const comments = await Comment.all();
            const pushArticle = await Article.select({ status: 1 });

            let { count } = req.query;

            /* 阅读总数 */
            let arr = articles.map(v => v.click);
            let read = eval(arr.join('+'));
            let days = Util.getDate(count);

            let talkData = utils.getChartDispose(days, talks);
            let commentData = utils.getChartDispose(days, comments);
            let pushArticleData = utils.getChartDispose(days, pushArticle);

            let sys = {
                tab: {
                    totalUser: users.length, // 用户总数
                    totalArticle: articles.length, // 文章总数
                    totalRelease: pushArticle.length, // 发布总数
                    totalReading: read, // 阅读总数
                },
                days,
                talkData,
                commentData,
                pushArticleData
            };

            res.json({ code: 200, msg: '链接成功', data: { sys } })
        } catch (e) {
            res.json({ code: 0, data: e })
        }
    }
}

module.exports = ControllerHome;